<?php

namespace App\Http\Middleware;

use App\Lib\SMSCode;
use App\Repositories\Eloquent\PlatSettingRepository;
use App\Services\RemitOrderService;
use App\Validators\Buz\SMSValidator;
use Closure;

class SMSPermit
{
    protected $smsValidate;
    protected $settingRepository;
    protected $remitService;

    public function __construct(SMSValidator $SMSValidator,
                                PlatSettingRepository $platSettingRepository,
                                RemitOrderService $remitOrderService)
    {
        $this->smsValidate = $SMSValidator;
        $this->settingRepository = $platSettingRepository;
        $this->remitService = $remitOrderService;
    }

    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $this->smsValidate->with($request->all())->passesOrFail('send');
        $type = $request->input('type', 1);
        if ($type == SMSCode::REMIT) {

            return (new RemitAble($this->settingRepository, $this->remitService))->handle($request, $next);
        }
        return $next($request);
    }
}
